<template>
  <div class="goods-container">
    <!-- 左侧图片区域 -->
    <div class="left">
      <img :src="item.thumb" class="avatar" alt="" />
    </div>
    <!-- 右侧商品区域 -->
    <div class="right">
      <!-- 标题 -->
      <div class="title">{{ item.name }}</div>
      <div class="info">
        <!-- 单价 -->
        <span class="price">￥{{ item.price }}</span>
        <div class="btns">
          <!-- 按钮区域 -->
          <button :disabled="item.count === 1" @click="changeCount(-1)" class="btn btn-light">-</button>
          <span class="count">{{ item.count }}</span>
          <button @click="changeCount(1)" class="btn btn-light">+</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CartItem',
  props: {
    item: {
      type: Object,
      required: true
    }
  },
  methods: {
    changeCount (num) {

      // 当前商品是什么，我就要让当前商品数量+1或者-1
      // 这里直接改vuex即可 --- 唯一允许修改state的地方是mutations
      // 所以要封装一个修改数量的mutations方法，还要发请求给接口修改数量，所以意味着还要有actions
      // 你让我修改，至少要告诉我修改哪一个，以及修改什么数量
      // 所以调用这些方法时要传入id和数量
      this.$store.dispatch('cart/changeCount', {
        id: this.item.id,
        count: this.item.count + num
      })
    }
  }
}
</script>

<style lang="less" scoped>
.goods-container {
  display: flex;
  padding: 10px;
  + .goods-container {
    border-top: 1px solid #f8f8f8;
  }
  .left {
    .avatar {
      width: 100px;
      height: 100px;
    }
    margin-right: 10px;
  }
  .right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    .title {
      font-weight: bold;
    }
    .info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .price {
        color: red;
        font-weight: bold;
      }
      .btns {
        .count {
          display: inline-block;
          width: 30px;
          text-align: center;
        }
      }
    }
  }
}

.custom-control-label::before,
.custom-control-label::after {
  top: 3.6rem;
}
</style>
